/**
 * 列转行
 * @param arr
 * @param isArea
 */
export const rowToColumns = (arr: any[], isArea = true) => {
  // 使用一个对象来存储日期和对应的分区数据
  const resultMap: Record<string, Record<string, any>> = {};

  arr.forEach((item) => {
    const {
      areaId,
      locationName,
      dischargeEnergy,
      chargeEnergy,
      convertRatio,
      pocOnGridEnergy,
      pocOffGridEnergy,
      pocConvertRatio,
      cycleType,
    } = item;
    const aimDate =
      cycleType === 'DAY' ? 'dataDate' : cycleType === 'MONTH' ? 'dataMonth' : 'dataYear';
    // 如果 date 不存在，初始化
    if (!resultMap[item[aimDate]]) {
      resultMap[item[aimDate]] = {
        areaId: areaId,
      };
      resultMap[item[aimDate]][aimDate] = item[aimDate];
    }

    // 使用 areaName 作为前缀，构造新的字段名
    const areaPrefix = locationName;
    if (isArea) {
      resultMap[item[aimDate]][`${areaPrefix}_dischargeEnergy`] = dischargeEnergy;
      resultMap[item[aimDate]][`${areaPrefix}_chargeEnergy`] = chargeEnergy;
      resultMap[item[aimDate]][`${areaPrefix}_convertRatio`] = convertRatio;
      resultMap[item[aimDate]][`${areaPrefix}_pocConvertRatio`] = pocConvertRatio;
      resultMap[item[aimDate]][`${areaPrefix}_pocOnGridEnergy`] = pocOnGridEnergy;
      resultMap[item[aimDate]][`${areaPrefix}_pocOffGridEnergy`] = pocOffGridEnergy;
    } else {
      resultMap[item[aimDate]][`${areaPrefix}_dischargeEnergy`] = dischargeEnergy;
      resultMap[item[aimDate]][`${areaPrefix}_chargeEnergy`] = chargeEnergy;
      resultMap[item[aimDate]][`${areaPrefix}_convertRatio`] = convertRatio;
    }
  });
  console.log('values', resultMap);
  // 将对象转换为数组
  return Object.values(resultMap);
};
